package com.mxchip.livestarmobile.mobile.origin;

import android.media.AudioFormat;
import android.media.AudioRecord;

import com.mxchip.livestarmobile.mobile.util.Log.MyLog;


/**
 * Created by Administrator on 2016/12/28.
 * 音频推送
 */

public class AudioPublisher {

    private static final String TAG = "AudioPublisher";
    private AudioRecord mic;
    private volatile AudioCallback audioCallback;
    private volatile boolean aloop = false;
    private Thread aworker;
    private int SAMPLERATE = 16000;//采样频率
    private static final int CHANNEL = AudioFormat.CHANNEL_IN_MONO;
    private static final int AUDIOFORMAT = AudioFormat.ENCODING_PCM_16BIT;//16位采样精度
    private int PUSH_AUDIO_LENGTH = 640;

    public AudioPublisher() {

    }

    public AudioPublisher(int samplerate) {
        this.SAMPLERATE = samplerate;
        this.PUSH_AUDIO_LENGTH = 1920;

    }

    public interface AudioCallback {
        void onGetPcmFrame(byte[] data, int size);

        void AudioOPenFail();
    }

    public void setAudioCallback(AudioCallback cb) {
        audioCallback = cb;
    }

    //开始推送音频
    public void startPublishAudio() {
        aworker = new Thread(new Runnable() {
            @Override
            public void run() {
                android.os.Process
                        .setThreadPriority(android.os.Process.THREAD_PRIORITY_AUDIO);
                startAudio();
            }
        });
        aloop = true;
        aworker.start();
    }

    //开启音频
    private void startAudio() {
        if (mic != null) {
            return;
        }
        try {
            mic = new AudioRecord(7, SAMPLERATE, CHANNEL, AUDIOFORMAT, PUSH_AUDIO_LENGTH);

            mic.startRecording();
           MyLog.logD(MyLog.AUDIO_SEND_MODEL,"AudioPublisher::"+"startAudio: "+mic.getRecordingState());
            while (mic.getRecordingState() != AudioRecord.RECORDSTATE_RECORDING) {
                // Wait until we can start recording...
            }

            while (aloop && !Thread.interrupted()) {
                byte[] pcmBuffer = new byte[PUSH_AUDIO_LENGTH];
                int ret = mic.read(pcmBuffer, 0, pcmBuffer.length);
                if (ret == AudioRecord.ERROR || ret == AudioRecord.ERROR_INVALID_OPERATION || ret == AudioRecord.ERROR_BAD_VALUE) {
                    stopMuc();
                    startAudio();
                    return;
                }
                if (audioCallback != null) {

                    audioCallback.onGetPcmFrame(pcmBuffer, pcmBuffer.length);
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
            stopMuc();
            if (audioCallback != null) {
                audioCallback.AudioOPenFail(/*"音频设备被占用\n请重新启动设备"*/);
            }
        }
    }

    //停止推送音频
    public void stopPublishAudio() {
        aloop = false;
        audioCallback = null;
        stopMuc();
        if (aworker != null) {
            aworker.interrupt();
            try {
                aworker.join();
            } catch (InterruptedException e) {
                e.printStackTrace();
                aworker.interrupt();
            }
            aworker = null;
        }


    }

    private void stopMuc() {
        if (mic != null) {
            mic.setRecordPositionUpdateListener(null);
            try {
                mic.stop();

            } catch (IllegalStateException e) {
                e.printStackTrace();
            } finally {
                if (mic != null) {
                    mic.release();
                    mic = null;
                }
            }
        }

    }


}
